﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebAPI_DEMO.Models.Database;
using WebAPI_DEMO.Models.Login_Register;
using WebAPI_DEMO.Service;

namespace WebAPI_DEMO.Controllers
{
    /// <summary>
    /// 授权验证
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]

    public class AuthenticationController : ControllerBase
    {
        //定义数据库上下文
        private readonly Store_2022Context _db;

        //注入jwt服务
        private readonly IJWTService _jwtService;
        /// <summary>
        /// 构造方法 构造注入
        /// 把数据库上下文初始化
        /// </summary>
        /// <param name="jwtService"></param>
        public AuthenticationController(Store_2022Context db, IJWTService jwtService)
        {
            _db = db;
            _jwtService = jwtService;
        }


        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name)
        {
            //返回  通过用户名 创建token
            return _jwtService.CreateToken(name,0);
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //标签 接口需要授权
        [Authorize]
        public string UserInfo()
        {
            //获取用户信息           登录用户 消息载荷 查询第一条（条件是Type==）   获取登录用户的用户名
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }


        /// <summary>
        /// 用户注册 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string RegisterAccount(RegisterAccountRequest request)
        {
            //判断用户名是否存在
            //唯一的用户名
            //当数据库存在这个用户名时 做出响应
            //存在
            var result = _db.Users.Any(x => x.UserName == request.UserName);
            if (result)
            {
                return "用户已存在！";
            }

            //添加数据
            //获取到请求参数了
            //使用参数
            //添加新用户
            var data = new User
            {
                UserName = request.UserName,
                Password = request.Password
            };
            _db.Users.Add(data);
            var row = _db.SaveChanges();
            if (row > 0)
                return "注册成功！";
            return "注册失败-";
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string LoginAccount(LoginAccountRequest request)
        {
            //登录 逻辑

            //校验用户

            //1、先把用户查询出来

            var user = _db.Users.FirstOrDefault(x => x.UserName == request.UserName);
            if (user == null)
                return "用户不存在";

            //2、校验密码
            if (user.Password != request.PassWord)
                return "密码不正确";

            //都正确 确认是这个人
            return _jwtService.CreateToken(user.UserName, user.UserId);
        }
    }
}

